//
//  DMShopManage.m
//  DMGME
//
//  Created by ihope99 on 15/8/31.
//  Copyright (c) 2015年 GYP. All rights reserved.
//

#import "DMShopManageController.h"
#import "TitleTypeView.h"
#import "NumbAdController.h"
#import "ShopStateController.h"
@interface DMShopManageController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation DMShopManageController
{
    TitleTypeView *_titleTypeView;
    UITableView *tableview;
    int _type;
    NSArray *arrStrLeft;
    NSArray *arrStrCenter;
    NSArray *arrStrRighe;
    
    NSMutableArray *magazineInfo;
    NSMutableArray *offprintsInfo;
    NSMutableArray *peripheryInfo;
    NSMutableArray *magazine_idArr;
    NSMutableArray *periphery_idArr;
    NSMutableArray *offprintsInfo_idArr;
    NSMutableArray *aroundArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商品管理";
    [self AddLeftImageBtn:[UIImage imageNamed:@"fanhui_03 2"]target:self action:@selector(GoBack)];
    
    arrStrLeft =@[@""];
    arrStrCenter =@[@""];
    arrStrRighe =@[@""];
 
    peripheryInfo = [NSMutableArray array];
    magazine_idArr = [NSMutableArray array];
    periphery_idArr = [NSMutableArray array];
    offprintsInfo_idArr = [NSMutableArray array];

    
    magazineInfo = [NSMutableArray array];
    offprintsInfo = [NSMutableArray array];
    aroundArr = [NSMutableArray array];
    //第一次加载
    [self magazineList:10];

    NSArray *aray=@[@"杂志",@"单行本",@"周边"];
      _titleTypeView=[[TitleTypeView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,40) WithBtuName:aray WithScrollView:nil] ;
    _titleTypeView.backgroundColor = [UIColor whiteColor];
    [_titleTypeView btuSelectWith:^(int tag) {
//        NSLog(@"--------%d",tag);
        magazineInfo = [NSMutableArray array];
        offprintsInfo = [NSMutableArray array];
        peripheryInfo = [NSMutableArray array];
        [tableview reloadData];
        
        _type=tag;
//        NSLog(@"====%d",_type     );
        if (_type == 0) {
        [self magazineList:_type];

        }else if (_type == 1){
        [self offprintsList:_type];

        }else{
        [self surroundingList:_type];
        }

    }];
    [self.view addSubview:_titleTypeView];
    
    tableview=[[UITableView alloc]initWithFrame:CGRectMake(0,42,self.view.frame.size.width, self.view.frame.size.height-42) style:UITableViewStylePlain];
    tableview.delegate=self;
    tableview.dataSource=self;
    tableview.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:tableview];
}


- (void)magazineList:(int)type
{
    [magazineInfo removeAllObjects];
    [magazine_idArr removeAllObjects];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    [dict setValue: @"listMagazine" forKey:@"action"];
    [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$listMagazine"] forKey:@"token"];
    [dict setValue: kkUserDict[@"id"] forKey:@"agent_id"];
//    NSLog(@"%@",kkUserDict[@"id"]);
    [self StartLoading];
    [AFDownManager POST:BookManageListNET parameters:dict success:^(id responseObject) {
//        NSLog(@"%@",responseObject);
//        NSLog(@"%@",responseObject[@"desc"]);
        [self StopLoading];
        if (magazine_idArr.count == 0 && magazineInfo.count == 0) {
            
       
        for (int i = 0; i<=[responseObject[@"data"][@"listData"] count]-1; i++) {
            NSString *bookname;
            NSString *magazine_id;
            
            bookname = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"listData"][i][@"bookname"]];
            magazine_id = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"listData"][i][@"magazine_id"]];
            [magazineInfo addObject:bookname];
            [magazine_idArr addObject:magazine_id];
        }
             }
        [tableview reloadData];
        
    } failure:^(NSError *error) {
        [self StopLoading];
    }];
}
- (void)offprintsList:(int)type{
    
    [offprintsInfo removeAllObjects];
    [offprintsInfo_idArr removeAllObjects];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [dict setValue: @"listOffprint" forKey:@"action"];
    [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$listOffprint"] forKey:@"token"];
    [dict setValue:kkUserDict[@"id"] forKey:@"agent_id"];
    [self StartLoading];
    [AFDownManager POST:BookManageListNET parameters:dict success:^(id responseObject) {
   //     NSLog(@"%@",responseObject);
        [self StopLoading];
        if (offprintsInfo.count == 0 && offprintsInfo_idArr.count == 0) {

        for (int i = 0; i<=[responseObject[@"data"][@"listData"] count]-1; i++) {
            NSString *bookname;
            NSString *magazine_id;
            
            bookname = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"listData"][i][@"bookname"]];
            magazine_id = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"listData"][i][@"magazine_id"]];
//            NSLog(@"bookName == %@  magazine_id == %@ ",bookname,magazine_id);
            [offprintsInfo addObject:bookname];
            [offprintsInfo_idArr addObject:magazine_id];
        }
        }
        [tableview reloadData];
        
    } failure:^(NSError *error) {
        
    }];

    
}
- (void)surroundingList:(int)type{
    
    [peripheryInfo removeAllObjects];
    [periphery_idArr removeAllObjects];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [dict setValue: @"listPeriphery" forKey:@"action"];
    [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$listPeriphery"] forKey:@"token"];
    [dict setValue:kkUserDict[@"id"] forKey:@"agent_id"];
    [self StartLoading];
    [AFDownManager POST:BookManageListNET parameters:dict success:^(id responseObject) {
//        NSLog(@"周边 responeObject = %@",responseObject);
        [self StopLoading];
        if (periphery_idArr.count == 0 && peripheryInfo.count == 0) {

        for (int i = 0; i<=[responseObject[@"data"][@"listData"] count]-1; i++) {
            NSString *bookname;
            NSString *magazine_id;
            NSString *aroundId = responseObject[@"data"][@"listData"][i][@"id"];
            bookname = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"listData"][i][@"title"]];
            magazine_id = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"listData"][i][@"id"]];
            [peripheryInfo addObject:bookname];
            [periphery_idArr addObject:magazine_id];
            [aroundArr addObject:aroundId];
        }
        }
        [tableview reloadData];
        
    } failure:^(NSError *error) {
        [self StopLoading];
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_type == 0) {
        return magazineInfo.count;
    }else if (_type == 1){
        return offprintsInfo.count;
    }else{
        return peripheryInfo.count;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ID =@"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID ];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    if (_type==0) {
        
        cell.textLabel.text =[NSString stringWithFormat:@"%@",magazineInfo[indexPath.row]];
    }else if(_type == 1)
    {
        cell.textLabel.text =[NSString stringWithFormat:@"%@",offprintsInfo[indexPath.row]];
       
    }else{
        cell.textLabel.text =[NSString stringWithFormat:@"%@",peripheryInfo[indexPath.row]];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
    NumbAdController *na = [[NumbAdController alloc] init];
    if (_type == 0) {
        na.magazine_id = magazine_idArr[indexPath.row];
       na.type_id = @"0";

        [self.navigationController pushViewController:na animated:YES];
    }else if (_type == 1)
    {
        na.type_id = @"1";
        na.magazine_id = offprintsInfo_idArr[indexPath.row];
//        NSLog(@"=== %@",na.magazine_id);
        [self.navigationController pushViewController:na animated:YES];
    }
else
{
    ShopStateController *shopState = [[ShopStateController alloc] init];
    shopState.numId = aroundArr[indexPath.row];
    shopState.periodInfoId = aroundArr[indexPath.row];
    shopState.type = _type;
//    NSLog(@"shopState.numId === %@ shopState.periodInfoId ===%@ shopState.type === %d",shopState.numId,shopState.periodInfoId,shopState.type);
    [self.navigationController pushViewController:shopState animated:YES];
}
    

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
